package com.aerospace.concurrent;

/**
 * @Author: Aerospace
 * @Date: 2021/5/7 8:49
 * @Description: Dream begins
 */
public class SingleInstance {
    private static SingleInstance singleInstance;
    private SingleInstance(){}

    //这里可能并发创建，第一步加synchronized，锁的粒度太大了
//    public static synchronized SingleInstance getSingleInstance(){
//        if (singleInstance==null){
//            singleInstance = new SingleInstance();
//        }
//        return singleInstance;
//    }

    //粒度缩小
    public static  SingleInstance getSingleInstance(){
        if (singleInstance==null){
            synchronized(SingleInstance.class){
                if (null==singleInstance){
                    singleInstance = new SingleInstance();
                }
            }
        }
        return singleInstance;
    }

}
